from django.contrib import admin
from .models import (
    NotificationTemplate, Notification, 
    EmailLog, SystemAnnouncement, UserNotificationPreference
)


@admin.register(NotificationTemplate)
class NotificationTemplateAdmin(admin.ModelAdmin):
    list_display = ['name', 'template_type', 'channel', 'is_active', 'created_at']
    list_filter = ['template_type', 'channel', 'is_active', 'created_at']
    search_fields = ['name', 'subject', 'content']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'template_type', 'channel', 'is_active')
        }),
        ('内容', {
            'fields': ('subject', 'content', 'variables')
        }),
        ('时间', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        })
    )


@admin.register(Notification)
class NotificationAdmin(admin.ModelAdmin):
    list_display = ['user', 'title', 'notification_type', 'is_read', 'priority', 'created_at']
    list_filter = ['notification_type', 'is_read', 'priority', 'created_at']
    search_fields = ['user__email', 'title', 'message']
    readonly_fields = ['user', 'created_at']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('user', 'notification_type', 'title', 'message')
        }),
        ('状态', {
            'fields': ('is_read', 'priority', 'expires_at')
        }),
        ('链接', {
            'fields': ('action_url',),
            'classes': ('collapse',)
        }),
        ('时间', {
            'fields': ('created_at',),
            'classes': ('collapse',)
        })
    )


@admin.register(EmailLog)
class EmailLogAdmin(admin.ModelAdmin):
    list_display = ['recipient', 'subject', 'status', 'sent_at', 'template_name']
    list_filter = ['status', 'sent_at', 'template_name']
    search_fields = ['recipient', 'subject', 'template_name']
    readonly_fields = ['recipient', 'sent_at']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('recipient', 'subject', 'status', 'template_name')
        }),
        ('内容', {
            'fields': ('body', 'error_message'),
            'classes': ('collapse',)
        }),
        ('时间', {
            'fields': ('sent_at',),
            'classes': ('collapse',)
        })
    )


@admin.register(SystemAnnouncement)
class SystemAnnouncementAdmin(admin.ModelAdmin):
    list_display = ['title', 'announcement_type', 'is_active', 'priority', 'start_date', 'end_date']
    list_filter = ['announcement_type', 'is_active', 'priority', 'start_date']
    search_fields = ['title', 'content']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('title', 'announcement_type', 'content', 'is_active')
        }),
        ('时间', {
            'fields': ('start_date', 'end_date')
        }),
        ('优先级', {
            'fields': ('priority',)
        }),
        ('链接', {
            'fields': ('action_url',),
            'classes': ('collapse',)
        })
    )


@admin.register(UserNotificationPreference)
class UserNotificationPreferenceAdmin(admin.ModelAdmin):
    list_display = ['user', 'email_notifications', 'push_notifications', 'sms_notifications', 'created_at']
    list_filter = ['email_notifications', 'push_notifications', 'sms_notifications', 'created_at']
    search_fields = ['user__email', 'user__username']
    readonly_fields = ['user', 'created_at']
    
    fieldsets = (
        ('用户', {
            'fields': ('user',)
        }),
        ('通知偏好', {
            'fields': ('email_notifications', 'push_notifications', 'sms_notifications')
        }),
        ('通知类型', {
            'fields': ('order_notifications', 'promotion_notifications', 'system_notifications')
        }),
        ('时间', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        })
    )
